package test.hystrix.client;

import java.util.Collection;
import java.util.Map;

import com.netflix.hystrix.HystrixCollapser;
import com.netflix.hystrix.HystrixCommand;
/**
 * -合并处理器
 * -HystrixCollapser 泛型参数
 * -1-批处理返回结果类型
 * -2-单独请求返回的结果类型
 * -3-请求的参数类型
 * @author hardy
 *
 */
public class MyHystrixCollapser extends HystrixCollapser<Map<String,String>, String, String>{
    private String conditionStr = null;
	public MyHystrixCollapser(String conditionStr) {
		this.conditionStr = conditionStr;
	}
	
	
	@Override
	protected String getCacheKey() {
		return this.conditionStr;
	}


	//返回参数
	@Override
	public String getRequestArgument() {
		// TODO Auto-generated method stub
		return this.conditionStr;
	}
    //返回命令对象
	@Override
	protected HystrixCommand<Map<String, String>> createCommand(Collection<CollapsedRequest<String, String>> requests) {
		// TODO Auto-generated method stub
		return new CollapserCommand(requests);   //此处返回一个HystrixComrnand 命令类的实现
	}
	//让结果与请求进行关联
	@Override
	protected void mapResponseToRequests(Map<String, String> batchResponse,
			Collection<CollapsedRequest<String, String>> requests) {
		for (CollapsedRequest<String,String> request : requests) {
			//获取单个请求返回结果
			String result = batchResponse.get(request.getArgument());
			//关联到请求中
			request.setResponse(result);
		}
	}

}
